using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;
using System.Xml;
using System.Text;
using System.IO;



namespace DotNetNuke.Modules.DnnHeader
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ViewDnnHeader class displays the content
    /// </summary>
    /// -----------------------------------------------------------------------------
    
    
    public partial class View : DnnHeaderModuleBase, IActionable
    {

        #region Event Handlers
        protected string sHeadText = "";
        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        /// 
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {

            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }

             int pid = PortalId;
            int mid = ModuleId;
            string path = Server.HtmlEncode(Request.PhysicalApplicationPath);
            string portalpath = "Portals\\" + pid.ToString();
            // Create a new XmlTextWriter instance
            string xpath = path.ToString() + portalpath.ToString() + "\\DNNHeaderData-" + pid.ToString() +"-"+mid.ToString()+"-.xml";
            FileStream fs = null;

            if (!File.Exists(xpath))
            {

                XmlTextWriter writer = new
                 XmlTextWriter(xpath.ToString(), Encoding.UTF8);

                // start writing!
                writer.WriteStartDocument();
                writer.Formatting = Formatting.Indented;
                writer.WriteStartElement("DNNHeaderdb");
                writer.WriteStartElement("HeaderDetails", "");
                writer.WriteElementString("Header", " ");

                writer.WriteEndElement();
                // Creating the <browserInfo> element

                writer.Close();
            }
            else 
            {
                string site = PortalAlias.HTTPAlias.ToString();
                int pidload = PortalId;
                int midload = ModuleId;
                string urlpath = Server.HtmlEncode(Request.PhysicalApplicationPath);
                string portalpathserver = "Portals\\" + pid.ToString();
                string xpathserver = urlpath.ToString() + portalpathserver.ToString() + "\\DNNHeaderData-" + pidload.ToString() + "-" + midload.ToString() + "-.xml";
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(xpath.ToString());
                string header = xdoc.GetElementsByTagName("Header")[0].InnerText;
                //dnncumtomheader.Text = header.ToString();
                //sHeadText = header.ToString();


                System.Web.UI.HtmlControls.HtmlHead head = Page.Header;

                System.Web.UI.LiteralControl lctl = new System.Web.UI.LiteralControl(header.ToString());

                head.Controls.Add(lctl);

                
            }
        }

           

        #endregion

        #region Optional Interfaces

        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), Localization.GetString("EditModule", this.LocalResourceFile), "", "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion

    }

}
